{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a5112619",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(4, 2) (4, 1)\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD4CAYAAAD8Zh1EAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAAPxElEQVR4nO3df6zddX3H8edrvUJpoNPYq7CCK1vwR6OyyC26ZQ46s0HxD2JiGJRoRkiaMlH+EzaDLjRmk2SJMSC1IYTsH5pmEkWDkiW7wAjr1tsEgUogXYlQ+XVR4xRpSMt7f5yLXi6393xv+d57ez88H8nNvd8fPef9SZsnX77n3ntSVUiSlr/fW+oBJEn9MOiS1AiDLkmNMOiS1AiDLkmNGFmqJ16zZk2tW7duqZ5ekpalvXv3vlhVo7MdW7Kgr1u3jomJiaV6eklalpL85GjHvOUiSY0w6JLUCIMuSY0w6JLUCIMuSY0YGvQktyV5IcmjRzmeJN9Isj/Jw0k+0v+Yv/Psr57lvNvP47lfP7eQTyNJvbrxRhgff/2+8fHB/r50uUK/HbhwjuObgLOmPrYAt7z5sY5u2/3beOCpB7jhvhsW8mkkqVcbNsAll/wu6uPjg+0NG/p7jnT59blJ1gHfr6oPznLsW8C9VXXH1PbjwPlV9excjzk2Nlbz+T70k756EocOH3rD/pUjK3n5Sy93fhxJWiqvRfyqq+CWW2DXLti4cX6PkWRvVY3NdqyPe+hrgaenbR+c2jfbIFuSTCSZmJycnNeTHPjCATZ/cDOrRlYBsGpkFZd/6HKevObJYxxbkhbXxo2DmG/bNvg835gP00fQM8u+WS/7q2pHVY1V1djo6Kw/uXpUp51yGqtPXM2hI4dYObKSQ0cOsfrE1Zx68qnHMrMkLbrx8cGV+fXXDz7PvKf+ZvUR9IPAG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f5ajLmq8A/rkGN5j3J3kSeD/wP4sz4qLrvV/H8y2XPcBZSc5McgJwKXDXjHPuAj479Wrxx4BfVtWziz1oj4auOcl7gDuBzyzjq7Xphq65qs6sqnVVtQ74N+DvlnHModu/7e8CH08ykmQVgzdff2yR5+xTlzU/xeD/SEjybuB9wIFFnXJx9d6v4/YKvY6TN6deTB3X/GXgncA3p65YD9cy/k11HdfclC5rrqrHkvwQeBh4Fbi1qmb99rfloOPf8zbg9iSPMLgdcW1VLdtfq5vkDuB8YE2Sg8BXgLfBwvXLH/2XpEYcz7dcJEnzYNAlqREGXZIaYdAlqREGXZIaYdAlqREGXZIa8f9rY2YcrDKAQgAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "plt.plot([0,1], [1,0], '*', color='green')\n",
    "plt.plot([0,1],[0,1], 'x', color='blue')\n",
    "\n",
    "x = np.array([[0,1],[1,0],[0,0],[1,1]])\n",
    "y = np.array([0,0,1,1]).reshape((-1,1))\n",
    "print(x.shape,y.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b5233561",
   "metadata": {},
   "source": [
    "异或(XOR)问题：按照逻辑学的解释，如果两个条件中任意一个为True，结果为True，如果两个条件都是True或都是False，结果为False。直观理解，将这四中情况在平面上展示出来，设定True=1,False=0,平面中4个点： (1:0,1),(1:1,0),(0:0,0),(0:1,1).\n",
    "\n",
    "上一节我们的logisti回归是找到一条直线，将平面里两种不同的点分割开来，而对于异或问题中的4个点，无论如何**找不到一条直线，将这2类点完美分割开来**。解决这类问题，就需要引入深度网络。\n",
    "\n",
    "深层网络，复合函数 g(f(x)), 让输入经过一个函数f，产出的结果再输入到另外一个函数g。如果两个函数都是线性的，得到复合函数也是线性的，仍然无法解决上面的问题，添加一些激活函数，让复合函数不再是简单的线性了。\n",
    "\n",
    "神经网络本质是多项式回归， DNN只要足够复杂，就能拟合任何一个多项式。\n",
    "\n",
    "反向传播算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b6ba7f3d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([4, 2])\n",
      "tensor([[0.],\n",
      "        [0.],\n",
      "        [1.],\n",
      "        [1.]])\n",
      "loss 0.28938162326812744\n",
      "epoch 0, loss 0.28938162326812744\n",
      "epoch 1, loss 0.2891395688056946\n",
      "epoch 2, loss 0.2888990044593811\n",
      "epoch 3, loss 0.288659930229187\n",
      "epoch 4, loss 0.28842228651046753\n",
      "epoch 5, loss 0.2881862223148346\n",
      "epoch 6, loss 0.2879515290260315\n",
      "epoch 7, loss 0.2877182960510254\n",
      "epoch 8, loss 0.2874864637851715\n",
      "epoch 9, loss 0.28725606203079224\n",
      "epoch 10, loss 0.2870270907878876\n",
      "epoch 11, loss 0.2867995500564575\n",
      "epoch 12, loss 0.2865734100341797\n",
      "epoch 13, loss 0.2863486707210541\n",
      "epoch 14, loss 0.2861253023147583\n",
      "epoch 15, loss 0.28590330481529236\n",
      "epoch 16, loss 0.28568270802497864\n",
      "epoch 17, loss 0.28546342253685\n",
      "epoch 18, loss 0.28524553775787354\n",
      "epoch 19, loss 0.28502899408340454\n",
      "epoch 20, loss 0.284813791513443\n",
      "epoch 21, loss 0.28459984064102173\n",
      "epoch 22, loss 0.2843872904777527\n",
      "epoch 23, loss 0.2841760218143463\n",
      "epoch 24, loss 0.2839660048484802\n",
      "epoch 25, loss 0.28375735878944397\n",
      "epoch 26, loss 0.283549964427948\n",
      "epoch 27, loss 0.2833437919616699\n",
      "epoch 28, loss 0.2831389904022217\n",
      "epoch 29, loss 0.28293538093566895\n",
      "epoch 30, loss 0.28273308277130127\n",
      "epoch 31, loss 0.2825319766998291\n",
      "epoch 32, loss 0.28233209252357483\n",
      "epoch 33, loss 0.282133549451828\n",
      "epoch 34, loss 0.2819361388683319\n",
      "epoch 35, loss 0.2817399799823761\n",
      "epoch 36, loss 0.2815449833869934\n",
      "epoch 37, loss 0.281351238489151\n",
      "epoch 38, loss 0.2811586558818817\n",
      "epoch 39, loss 0.2809672951698303\n",
      "epoch 40, loss 0.28077706694602966\n",
      "epoch 41, loss 0.2805880606174469\n",
      "epoch 42, loss 0.28040021657943726\n",
      "epoch 43, loss 0.28021347522735596\n",
      "epoch 44, loss 0.28002792596817017\n",
      "epoch 45, loss 0.2798435091972351\n",
      "epoch 46, loss 0.27966028451919556\n",
      "epoch 47, loss 0.2794781029224396\n",
      "epoch 48, loss 0.2792970538139343\n",
      "epoch 49, loss 0.2791171371936798\n",
      "epoch 49, loss 0.2791171371936798\n",
      "state_dict: OrderedDict([('linear_1.weight', tensor([[ 0.1627,  0.5870],\n",
      "        [ 0.2699, -0.6244]])), ('linear_1.bias', tensor([ 0.1096, -0.2457])), ('linear_2.weight', tensor([[ 0.1433, -0.0212]])), ('linear_2.bias', tensor([0.5894]))])\n",
      "[Parameter containing:\n",
      "tensor([[ 0.1627,  0.5870],\n",
      "        [ 0.2699, -0.6244]], requires_grad=True), Parameter containing:\n",
      "tensor([ 0.1096, -0.2457], requires_grad=True), Parameter containing:\n",
      "tensor([[ 0.1433, -0.0212]], requires_grad=True), Parameter containing:\n",
      "tensor([0.5894], requires_grad=True)]\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXwAAAD4CAYAAADvsV2wAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAAhOUlEQVR4nO3deXCc9Z3n8fdX923JknzpsAyY01y2bJMMCTjHBEgmTGYShsAmOyy7HrMhyezMTshOKjNTUKlh2NrdJAWBcbEUSe1WGDZhJiTLJJvZmJBsAEsmYDAEYnzK8iHZ8iV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+g3sxeAq4FcC/xUtuUu4EEPBsJ3mtlu4FJg69x0MWPmy3c+joZ0ZtYBrDSzFWZWAtwOPJuwzLPAZyNn7q8DTrj7wbnuaApm3BYzawWeAT6Tg0eQ42bcDndf4e5t7t4GfA/49zkY9pDa39cPgPeZWZGZVQDrgbfmuJ+pSGVb9hH8UsHMFgOXALvmtJeZMV++83F0hD8Ddx8xs3uBnxBUITzh7jvMbFOk/TGCKpBbgJ3AAMFRTM5JcVv+CqgHvhU5Oh7xHJsZMMXtmBdS2RZ3f8vMfgxsB8aAx909ablgNqX4/+UB4Ekze51gWOQ+d8+5qYbN7LvAjUCDmXUBfw0Uw/z6zifS1AoiIiGhIR0RkZBQ4IuIhIQCX0QkJBT4IiIhocAXEQkJBb6ISEgo8EVEQuL/A8mlZZH3jhbyAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import torch \n",
    "import torch.nn as nn\n",
    "\n",
    "# 创建一个Xor的模型\n",
    "class XOrModel(nn.Module):\n",
    "    def __init__(self, input_dim, output_dim):\n",
    "        super(XOrModel, self).__init__()\n",
    "        hidden_dim = 2\n",
    "        self.linear_1 = nn.Linear(input_dim, hidden_dim,bias=True)  \n",
    "        self.linear_2 = nn.Linear(hidden_dim, output_dim,bias=True)  \n",
    "\n",
    "    def forward(self, x):\n",
    "        out = torch.sigmoid(self.linear_1(x))\n",
    "#         out = torch.nn.functional.relu(self.linear_1(x))\n",
    "        out = self.linear_2(out)\n",
    "        return out\n",
    "\n",
    "learning_rate = 0.001\n",
    "\n",
    "model = XOrModel(2, 1) #模型初始化\n",
    "criterion = nn.MSELoss() #定义损失函数\n",
    "optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate) #定义最优化算法\n",
    "\n",
    "inX = torch.as_tensor(x,dtype=torch.float32) #将numpy转成tensor\n",
    "outY = torch.as_tensor(y,dtype=torch.float32)\n",
    "print(inX.shape)\n",
    "print(outY)\n",
    "\n",
    "predict_Y = model(inX) #根据输入获得当前参数下的输出值\n",
    "loss = criterion(predict_Y, outY) #计算误差\n",
    "print('loss {}'.format(loss.item()))\n",
    "\n",
    "for epoch in range(50):  #迭代次数\n",
    "    optimizer.zero_grad() #清理模型里参数的梯度值\n",
    "    predict_Y = model(inX) #根据输入获得当前参数下的输出值\n",
    "    loss = criterion(predict_Y, outY) #计算误差\n",
    "    loss.backward() #反向传播，计算梯度，\n",
    "    optimizer.step() #更新模型参数\n",
    "#     if epoch % 50 ==0:\n",
    "    print('epoch {}, loss {}'.format(epoch, loss.item()))\n",
    "\n",
    "print('epoch {}, loss {}'.format(epoch, loss.item()))\n",
    "\n",
    "delta = 1e-7\n",
    "print(\"state_dict:\",model.state_dict())\n",
    "\n",
    "plt.plot([0,1], [1,0], '*', color='green')\n",
    "plt.plot([0,1],[0,1], 'x', color='blue')\n",
    "\n",
    "\n",
    "# 怎么画出分割面，还是没搞懂，待琢磨\n",
    "model_params = list(model.parameters())\n",
    "print(model_params)\n",
    "model_weights = model_params[0].data.numpy()\n",
    "model_bias = model_params[1].data.numpy()\n",
    "\n",
    "x_1 = np.arange(-0.1, 1.1, 0.1)\n",
    "y_1 = ((x_1 * model_weights[0,0]) + model_bias[0]) / (-model_weights[0,1])\n",
    "plt.plot(x_1, y_1)\n",
    "\n",
    "x_11 = np.arange(-0.1, 1.1, 0.1)\n",
    "y_11 = ((x_11 * model_weights[1,0]) + model_bias[0]) / (-model_weights[1,1])\n",
    "plt.plot(x_11, y_11)\n",
    "\n",
    "# x_2 = np.arange(-0.1, 1.1, 0.1)\n",
    "# y_2 = ((x_2 * model_weights[1,0]) + model_bias[1]) / (-model_weights[1,1])\n",
    "# plt.plot(x_2, y_2)\n",
    "plt.show()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b778bc47",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-0.1   -0.099 -0.098 ...  1.098  1.099  1.1  ] [0. 0. 0. ... 0. 0. 0.]\n",
      "``````\n",
      "[[-0.1    0.   ]\n",
      " [-0.099  0.   ]\n",
      " [-0.098  0.   ]\n",
      " ...\n",
      " [ 1.098  0.   ]\n",
      " [ 1.099  0.   ]\n",
      " [ 1.1    0.   ]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x7fd2d02f0220>]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD4CAYAAAD8Zh1EAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAAQtklEQVR4nO3df4xdZZ3H8feHjlAaqBo7Cgu4ZTf4o1HZyBR1o0u7ZheKfxCjYaFEs4SkFkX5T9g16EpjdiXZxBiQ2hhC9h8JWYiiQckmO8ASYLfTiEA1mG6JUPk1qHEVaUjhu3/cQabjdO5puTN37sP7ldzce57zzLnfpzP3k6fPveeeVBWSpNF31LALkCQNhoEuSY0w0CWpEQa6JDXCQJekRowN64nXrFlTa9euHdbTS9JI2rVr17NVNT7fvqEF+tq1a5mamhrW00vSSEry80Ptc8lFkhphoEtSIwx0SWqEgS5JjTDQJakRfQM9yQ1Jnkny8CH2J8nXk+xJ8mCS9w6+zKX35G+f5Kwbz+Kp3z017FIkLaJrroHJyYPbJid77aOmywz9RuCcBfZvAk6buW0Brn/1ZQ3ftru3cc9j93D1XVcPuxRJi2j9ejj//FdCfXKyt71+/XDrOhLp8vW5SdYC36+qd82z75vAnVX17ZntR4ANVfXkQsecmJio5fg59GO/ciz7D+z/o/aVYyt5/gvPD6EiSYvt5RC/9FK4/nq4+WbYuHHYVc0vya6qmphv3yDW0E8CHp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JrCnqvZW1QvATcB5c/qcB/xb9dwPvCHJiUtdaAd9x1JV91bVr2c276d3ZanlqMvvBeCzwC3AM0tZ3GHqMpbNwK1V9RhAVS3H8XQZRwHHJwlwHL1AP7C0ZXZTVXfTq+9QRuV1f5DXeqAP7ALYy8Dh1nkJvRnIctR3LElOAj4KbGd56/J7eRvwxiR3JtmV5JNLVl13XcZxLfBOepeffAi4vKpeWpryBm5UXvcH6XSBi4YN7ALYy0DnOpNspBfoH1zUio5cl7F8Dbiiql7sTQiXrS5jGQPOoHeZx2OB+5LcX1U/W+ziDkOXcZwNPAD8NfDnwH8k+a+q+r9Frm0xjMrr/iCv9UBv6QLYnepM8h7gW8CmqvrlEtV2uLqMZQK4aSbM1wDnJjlQVd9Zkgq76/o39mxVPQc8l+Ru4HR61/JdLrqM42LgX6q3CL0nyaPAO4D/WZoSB2pUXvcHea0vuewETktyapKjgQuA2+b0uQ345My73u8HflNVTy51oR30HUuStwK3Ap9YZrO/ufqOpapOraq1VbUW+Hfg08swzKHb39h3gQ8lGUuyit6F1n+6xHX202Ucj9H7XwZJ3gK8Hdi7pFUOzqi87g/ymp6hV0MXwO44li8CbwK+MTOzPVDL8JvlOo5lJHQZS1X9NMkPgQeBl4BvVdW8H6cblo6/k23AjUkeordkcUVVLcuvok3ybWADsCbJPuBLwOtgtF73c3nqvyQ14rW+5CJJzTDQJakRBrokNcJAl6RGGOiS1AgDXZIaYaBLUiP+H1tKMlUeEhV+AAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "x_11 = np.arange(-0.1, 1.1, 0.001)\n",
    "tmp = np.zeros_like(x_11)\n",
    "print(x_11,tmp)\n",
    "print(\"``````\")\n",
    "tt = np.concatenate((x_11.reshape((-1,1)),tmp.reshape(-1,1)),axis=1)\n",
    "print(tt)\n",
    "y = model(torch.tensor(tt,dtype=torch.float32))\n",
    "# print()\n",
    "y1 = y.data.numpy().flatten()\n",
    "\n",
    "plt.plot([0,1], [1,0], '*', color='green')\n",
    "plt.plot([0,1],[0,1], 'x', color='blue')\n",
    "plt.plot(x_11,y1, '.', color='blue')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c19b39f",
   "metadata": {},
   "source": [
    "y = w1*x**2 + w2*x + b ,深度网络对于多项式的拟合"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
